package com.fly.hello.job;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
@EnableScheduling
public class ScheduleJob
{
    /**
     * 无默认值会抛出IllegalArgumentException
     */
    @Value("${welcome.msg:hello, 00fly!}")
    private String welcome;
    
    @Bean
    public ScheduledExecutorService scheduledExecutorService()
    {
        return Executors.newScheduledThreadPool(10);
    }
    
    /**
     * 7-18点每10秒执行<br>
     * 
     */
    @Scheduled(cron = "0/10 * 7-18 * * ?")
    public void run1()
    {
        try
        {
            TimeUnit.SECONDS.sleep(5); // 注意这儿的休眠时间
            log.info("{}", welcome);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    /**
     * fixedDelay 7秒
     */
    @Scheduled(fixedDelay = 7000)
    public void run2()
    {
        try
        {
            TimeUnit.SECONDS.sleep(5); // 注意这儿的休眠时间
            log.info("{}", welcome);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
